﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Convertor.Interface;
using Wpf.CommonTools.StaticExtension;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.SendEmail
{
    /// <summary>
    /// ConfigForm.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigForm : PluginConfigForm
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void AddTarget_Click(object sender, RoutedEventArgs e)
        {
            openDialog(new SmtpClientViewModel(), addNewTarget);
        }

        private void ModifyTarget_Click(object sender, RoutedEventArgs e)
        {
            SmtpClientViewModel sv = ((Button)sender).Tag as SmtpClientViewModel;
            if (sv != null)
            {
                openDialog(sv, addNewTarget);
            }
        }

        private void DeleteTarget_Click(object sender, RoutedEventArgs e)
        {
            SmtpClientViewModel sv = ((Button)sender).Tag as SmtpClientViewModel;
            if (sv != null)
            {
                if (MessageBox.Show(UILanguage.FindResource("Plugin_SendEmail_DeleteSMTPDesc"),
                    UILanguage.FindResource("Plugin_SendEmail_DeleteSMTPTitle"),
                    MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    var a = config.SmtpClientList.Select((v, i) => new { Model = v, Index = i }).FirstOrDefault(p => p.Model.Id == sv.Id);
                    if (a == null || a.Index == -1)
                    {
                        MessageBox.Show(UILanguage.FindResource("Plugin_SendEmail_NotExists"));
                    }
                    else
                    {
                        config.SmtpClientList.RemoveAt(a.Index);
                    }
                }
            }

        }

        private void openDialog(SmtpClientViewModel v, EventHandler c)
        {
            SmtpClientWindows wSmtp = new SmtpClientWindows();
            wSmtp.Config = v;
            if (ParentWindow != null)
            {
                wSmtp.SetCenterOfWindow(ParentWindow);
            }
            wSmtp.Closed += c;
            wSmtp.ShowDialog();
        }

        private void addNewTarget(object sender, EventArgs e)
        {
            if (((SmtpClientWindows)sender).DialogResult == true)
            {
                SmtpClientViewModel sv = ((SmtpClientWindows)sender).Config;
                var a = config.SmtpClientList.Select((v, i) => new { Model = v, Index = i }).FirstOrDefault(p => p.Model.Id == sv.Id);
                if (a == null || a.Index == -1)
                {
                    ((SmtpClientWindows)sender).Dispatcher.Invoke(() => { config.SmtpClientList.Add(sv); });
                }
                else
                {
                    ((SmtpClientWindows)sender).Dispatcher.Invoke(() => { config.SmtpClientList[a.Index] = a.Model; });
                }
            }
        }

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config ?? new ConfigContent(); }
            set { config = (value as ConfigContent) == null ? new ConfigContent() : (ConfigContent)((ICloneable)value).Clone(); }
        }

        #endregion

        #region IConfigFormEvent 成员

        public override object GetConfig()
        {
            SendEmail.SmtpClientList.Clear();
            return ((ConfigContent)Config).Clone();
        }

        #endregion

        #region IPluginHistory 成员

        public override object Input { get; protected set; }

        #endregion

    }
}
